/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.rpc.isfd;

import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.json.utils.JsonUtils;
import com.comac.ins.isfd.rpc.isfd.bo.*;
import com.comac.ins.isfd.rpc.isfd.config.IsfdRequestConfig;
import com.comac.ins.isfd.rpc.isfd.constants.IsfdConstants;
import com.comac.ins.system.service.ISysDictDataService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class IsfdRpcUtils {

    @Autowired
    private IsfdRestTemplateUtils isfdRestTemplateUtils;

    @Autowired
    private IsfdRequestConfig isfdRequestConfig;

    @Autowired
    private ISysDictDataService iSysDictDataService;

    public void isfdModelSave(IsfdModelSaveBo isfdModelSaveBo) {
        // 1.获取建模后的精细模型结构管理模型库信息配置
        String modelConfig = iSysDictDataService.selectDictValueByTypeAndLabel(IsfdConstants.MODEL_RENDER_CONSTANTS, IsfdConstants.MODEL_CONFIG);
        if(StringUtils.isBlank(modelConfig)){
            throw new BaseException("数据库建模后，字典未配置 模型组装渲染的 精细模型结构管理模型库信息，请配置后重试！");
        }
        IsfdModelThirdPartyDatabaseConfigBo isfdModelThirdPartyDatabaseConfigBo = JsonUtils.parseObject(modelConfig, IsfdModelThirdPartyDatabaseConfigBo.class);
        // 2.数据库建模后，获取数据库建模配置
        String configUrl = isfdRequestConfig.getPreUrl() + IsfdConstants.ISFD_MODEL_THIRD_PARTY_DATABASE_CONFIG;
        IsfdResponseBo configResult = isfdRestTemplateUtils.post(configUrl, isfdModelThirdPartyDatabaseConfigBo);
        if (configResult.getData() == null) {
            log.error("数据库建模后，获取数据库建模配置的data值为空,configUrl：{}, 入参为：{}", configUrl, JsonUtils.toJsonString(isfdModelThirdPartyDatabaseConfigBo));
            throw new BaseException("数据库建模后，获取数据库建模配置的data值为空");
        }
        IsfdModelThirdPartyDatabaseConfigResultBo configResultBo = JsonUtils.parseObject(configResult.getData(), IsfdModelThirdPartyDatabaseConfigResultBo.class);
        if (configResultBo == null || StringUtils.isBlank(configResultBo.getTableName())) {
            log.error("数据库建模后，获取数据库建模配置的tableName值为空,configUrl：{}, 入参为：{}", configUrl, JsonUtils.toJsonString(isfdModelThirdPartyDatabaseConfigBo));
            throw new BaseException("数据库建模后，获取数据库建模配置的tableName值为空");
        }
        // 3.数据库建模后，数据批量保存接口
        String saveUrl = isfdRequestConfig.getPreUrl() + IsfdConstants.ISFD_MODEL_THIRD_PARTY_SAVE;
        IsfdModelThirdPartySaveBo isfdModelThirdPartySaveBo = new IsfdModelThirdPartySaveBo();
        isfdModelThirdPartySaveBo.setTableName(configResultBo.getTableName());
        List<Object> entityListJson = new ArrayList<>();
        entityListJson.add(isfdModelSaveBo);
        isfdModelThirdPartySaveBo.setEntityListJson(entityListJson);
        isfdRestTemplateUtils.post(saveUrl, isfdModelThirdPartySaveBo);
    }
}
